﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;
using System.Collections;
using System.Collections.Generic;
using Tesla.Graphics;
using XFG = Microsoft.Xna.Framework.Graphics;

namespace Tesla.Xna.Graphics {
    /// <summary>
    /// Concrete XNA 4.0 implementation of <see cref="IEffectTechniqueCollection"/>.
    /// </summary>
    public sealed class XNAEffectTechniqueCollection : IEffectTechniqueCollection {
        private List<XNAEffectTechnique> _techniques;

        /// <summary>
        /// Gets the number of techniques in the collection.
        /// </summary>
        public int Count {
            get {
                return _techniques.Count;
            }
        }

        /// <summary>
        /// Gets the technique specified by its index.
        /// </summary>
        /// <returns>The technique, if it exists</returns>
        public IEffectTechnique this[int index] {
            get {
                if(index >= 0 && index < _techniques.Count) {
                    return _techniques[index];
                }
                return null;
            }
        }

        /// <summary>
        /// Gets the technique specified by its index.
        /// </summary>
        /// <returns>The technique, if it exists</returns>
        public IEffectTechnique this[String name] {
            get {
                for(int i = 0; i < _techniques.Count; i++) {
                    IEffectTechnique tech = _techniques[i];
                    if(tech.Name.Equals(name)) {
                        return tech;
                    }
                }
                return null;
            }
        }


        /// <summary>
        /// Creates a new instance of <see cref="XNAEffectTechniqueCollection"/>.
        /// </summary>
        /// <param name="coll">The underlying XNA effect technique collection.</param>
        internal XNAEffectTechniqueCollection(XFG.EffectTechniqueCollection coll) {
            _techniques = new List<XNAEffectTechnique>(coll.Count);
            for(int i = 0; i < coll.Count; i++) {
                _techniques.Add(new XNAEffectTechnique(coll[i]));
            }
        }


        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator<IEffectTechnique> GetEnumerator() {
            return (IEnumerator<IEffectTechnique>) _techniques.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator() {
            return (IEnumerator) _techniques.GetEnumerator();
        }
    }
}
